package demo.calcite;

import demo.calcite.parser.SqlParserImpl;
import org.apache.calcite.avatica.util.Casing;
import org.apache.calcite.avatica.util.Quoting;
import org.apache.calcite.config.Lex;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlNode;
import org.apache.calcite.sql.SqlSelect;
import org.apache.calcite.sql.parser.SqlParseException;
import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.validate.SqlConformanceEnum;

/**
 * @author 支绍昆
 * 2022/12/27
 **/
/*

 */
public class Main {

    private final static String sql1 = "select a from table_one where a='1'";
    private final static String sql2 = "select a,b,c,d from table_one ma left join table_tow sl on ma.j = sl.j where ma.a = '1' ";

    public static void main(String[] args) throws SqlParseException {
        sql1();
    }

    private static void example() throws SqlParseException {
        SqlParser.Config mysqlConfig = SqlParser.configBuilder().setLex(Lex.MYSQL).build();
        SqlParser sqlParser = SqlParser.create(sql1, mysqlConfig);
        SqlNode sqlNode = sqlParser.parseQuery();

        SqlKind kind = sqlNode.getKind();
        SqlParserPos parserPosition = sqlNode.getParserPosition();
        String s1 = sqlNode.toString();
        System.out.println(sqlNode.toString());

        SqlSelect select = (SqlSelect) sqlNode;
        System.out.println(select.getFrom());
    }

    private static void sql1() throws SqlParseException {
        String sql ="select dscode as 集团编码\n" +
                ",dsname as 集团名称\n" +
                ",case when dscode = '990001' then '实业总公司' \n" +
                "      when dscode = '780000' then '浙江海工'\n" +
                "      when dscode = '790000' then '杭州意能'\n" +
                "      else concat(substr(dsname,3,2),'集团')\n" +
                " end as '集团简称'\n" +
                ",cyear as '年份'\n" +
                ",cperiod as '月份'\n" +
                ",hc as '用工人数'\n" +
                "from ADS.ads_hr_empl_bd_psndoc_dsygtj";
//        SqlParserImpl sqlParser = new SqlParserImpl();

        SqlParser.Config configBuilder = SqlParser.configBuilder()
                .setParserFactory(SqlParserImpl.FACTORY)
                .setQuotedCasing(Casing.UNCHANGED)
                .setUnquotedCasing(Casing.UNCHANGED)
                .setConformance(SqlConformanceEnum.LENIENT)
                .setCaseSensitive(true)
                .setQuoting(Quoting.BRACKET).build();
        SqlNode sqlNode1 = SqlParser.create(sql, configBuilder).parseQuery();
        System.out.println(sqlNode1);
    }

}
